local skel = fk.CreateSkill {
  name = "rmt__chanyan",
}

Fk:loadTranslationTable{
  ["rmt__chanyan"] = "谗焰",
  [":rmt__chanyan"] = "出牌阶段开始时，你可以摸三张牌，令本阶段你仅能将两张即时牌当其中一张牌使用，且以此牌可多选择一个目标（无视合法性）。",

  ["@@rfenghou__chanyan-phase"] = "谗焰",
  ["rmt__chanyan_viewas"] = "谗焰：将两张即时牌当其中一张牌使用，选择要使用的牌（1/2）",
  ["rmt__chanyan_sub"] = "谗焰：将两张即时牌当【%arg】使用，选择底牌（2/2）",
  ["rmt__chanyan_extra"] = "谗焰：可以为 %arg 多选择一个目标（无视合法性）",
}


skel:addEffect("viewas", {
  anim_type = "negative",
  pattern = ".|.|.|.|.|basic,trick",
  prompt = function (self, player, selected_cards, selected)
    if #selected_cards == 0 then
      return skel.name.."_viewas"
    else
      return skel.name.."_sub:::" .. Fk:getCardById(selected_cards[1]).name
    end
  end,
  card_num = 2,
  card_filter = function (self, player, to_select, selected)
    local card = Fk:getCardById(to_select)
    if #selected < 2 and (card.type == Card.TypeBasic or card:isCommonTrick()) then
      if #selected == 0 then
        local temp = Fk:cloneCard(card.name)
        temp.skillName = skel.name
        if Fk.currentResponsePattern then
          return Exppattern:Parse(Fk.currentResponsePattern):matchExp(".|.|.|.|" .. card.name)
        else
          return player:canUse(temp)
        end
      end
      return true
    end
  end,
  view_as = function (self, player, cards)
    if #cards ~= 2 then return end
    local c = Fk:cloneCard(Fk:getCardById(cards[1]).name)
    c.skillName = skel.name
    c:addSubcards(cards)
    return c
  end,
  enabled_at_play = function (self, player)
    return player:getMark("@@rfenghou__chanyan-phase") ~= 0
  end,
  enabled_at_response = function (self, player, response)
    -- FIXME: 闪可以用出来，但是无懈不行，不知道为啥
    return not response and player:getMark("@@rfenghou__chanyan-phase") ~= 0
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and player.phase == Player.Play
  end,
  on_use = function (self, event, target, player, data)
    player:drawCards(3, skel.name)
    if player.dead then return end
    player.room:setPlayerMark(player, "@@rfenghou__chanyan-phase", 1)
  end
})

-- 额，非即时牌不受限制，可以正常用
skel:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if card.type == Card.TypeBasic or card:isCommonTrick() then
      return player:getMark("@@rfenghou__chanyan-phase") ~= 0 and not table.contains(card.skillNames, skel.name)
    end
  end,
})

skel:addEffect(fk.AfterCardTargetDeclared, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and table.contains(data.card.skillNames, skel.name) and not data.toCard
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return not table.contains(data.tos, p)
    end)
    if #targets == 0 then return end
    local tos = room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, skill_name = skel.name, cancelable = true, targets = targets,
      prompt = "rmt__chanyan_extra:::" .. data.card:toLogString()
    })
    if #tos == 0 then return end
    data:addTarget(tos[1])
  end,
})

return skel
